package GameUI;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
	int[][] picTwoDemention = new int[4][4];
	int x = 0;
	int y = 0;
	String path = "image\\animal\\animal3\\";
	int step = 0;

	JMenuItem replayGame = new JMenuItem("重新游戏");
	JMenuItem reLoginGame = new JMenuItem("重新登录");
	JMenuItem closeGame = new JMenuItem("关闭游戏");
	JMenuItem accountItem = new JMenuItem("公众号");
	JMenuItem girls = new JMenuItem("美女");
	JMenuItem animal = new JMenuItem("动物");
	JMenuItem sports = new JMenuItem("运动");

	public GameJFrame() {
		//初始化界面
		InitJfram();
		//初始化菜单
		InitMenu();
		initData();
		initImage();
		//显示出来
		this.setVisible(true);
	}

	private void initImage() {
		this.getContentPane().removeAll();
		if (victory()) {
			JLabel winjLabel = new JLabel(new ImageIcon("image/win.png"));
			winjLabel.setBounds(203, 283, 197, 73);
			this.getContentPane().add(winjLabel);
		}
		//添加步数效果
		JLabel stepCount = new JLabel("步数：" + step);
		stepCount.setBounds(50, 30, 100, 20);
		this.getContentPane().add(stepCount);

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				JLabel jLabel = new JLabel(new ImageIcon(path + picTwoDemention[i][j] + ".jpg"));
				jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
				jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
				this.getContentPane().add(jLabel);
			}
		}

		JLabel background = new JLabel(new ImageIcon("image/background.png"));
		background.setBounds(40, 40, 508, 560);
		this.getContentPane().add(background);
		this.getContentPane().repaint();
	}

	private void initData() {
		int[] picNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17};
		Random rd = new Random();
		for (int i = 0; i < picNumber.length; i++) {
			int index = rd.nextInt(picNumber.length);
			int temp = picNumber[index];
			picNumber[index] = picNumber[i];
			picNumber[i] = temp;
		}
		int picNumberCount = 0;

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (picNumber[picNumberCount] == 17) {
					x = i;
					y = j;
				}
				picTwoDemention[i][j] = picNumber[picNumberCount];
				picNumberCount++;
			}
		}
	}

	//初始化菜单
	private void InitJfram() {
		//宽高
		this.setSize(603, 680);
		this.setTitle("拼图游戏界面");
		this.setAlwaysOnTop(true);
		//设置界面居中
		this.setLocationRelativeTo(null);
		//设置关闭模式
		this.setDefaultCloseOperation(3);
		//取消默认布局
		this.setLayout(null);
		this.addKeyListener(this);
	}

	private void InitMenu() {
		//初始化菜单
		JMenuBar jMenuBar = new JMenuBar();
		JMenu functionMenu = new JMenu("功能");
		JMenu aboutMenu = new JMenu("关于我们");
		JMenu changePicture = new JMenu("更换图片");
		//添加更换图片
		changePicture.add(girls);
		changePicture.add(animal);
		changePicture.add(sports);
		functionMenu.add(changePicture);
		//添加重新游戏，重新登录，关闭游戏
		functionMenu.add(replayGame);
		functionMenu.add(reLoginGame);
		functionMenu.add(closeGame);
		aboutMenu.add(accountItem);
		jMenuBar.add(functionMenu);
		jMenuBar.add(aboutMenu);
		//绑定事件
		replayGame.addActionListener(this);
		reLoginGame.addActionListener(this);
		closeGame.addActionListener(this);
		accountItem.addActionListener(this);
		//更换图片绑定事件
		girls.addActionListener(this);
		animal.addActionListener(this);
		sports.addActionListener(this);

		//设置菜单
		this.setJMenuBar(jMenuBar);
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

	@Override
	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		if (code == 65) {
			this.getContentPane().removeAll();
			JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));
			all.setBounds(83, 134, 420, 420);
			this.getContentPane().add(all);
			JLabel background = new JLabel(new ImageIcon("image/background.png"));
			background.setBounds(40, 40, 508, 560);
			this.getContentPane().add(background);
			this.getContentPane().repaint();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (victory()) return;
		int code = e.getKeyCode();
		switch (code) {
			case 37:
				if (y + 1 <= 3) {
					swap(x, x, y, y + 1, picTwoDemention);
					y++;
					step++;
				}
				break;
			case 38:
				if (x + 1 <= 3) {
					swap(x, x + 1, y, y, picTwoDemention);
					x++;
					step++;
				}
				break;
			case 39:
				if (y - 1 >= 0) {
					swap(x, x, y - 1, y, picTwoDemention);
					y--;
					step++;
				}
				break;
			case 40:
				if (x - 1 >= 0) {
					swap(x - 1, x, y, y, picTwoDemention);
					x--;
					step++;
				}
				break;
			case 65:
				initImage();
				break;
			case 87:
				picTwoDemention = new int[][]{
						{1, 2, 3, 4},
						{5, 6, 7, 8},
						{9, 10, 11, 12},
						{13, 14, 15, 16}
				};
				initImage();
			default:
		}
		initImage();
	}

	private boolean victory() {
		int[][] win = {
				{1, 2, 3, 4},
				{5, 6, 7, 8},
				{9, 10, 11, 12},
				{13, 14, 15, 16}
		};
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (picTwoDemention[i][j] != win[i][j]) return false;
			}
		}
		return true;
	}

	public void swap(int i1, int i2, int j1, int j2, int[][] arr) {

		int temp = arr[i1][j1];
		arr[i1][j1] = arr[i2][j2];
		arr[i2][j2] = temp;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if (obj == replayGame) {
			initData();
			step = 0;
			initImage();
		} else if (obj == reLoginGame) {
			this.setVisible(false);
			new LoginJFrame();
		} else if (obj == closeGame) {
			//直接关闭虚拟器
			System.exit(0);
		} else if (obj == accountItem) {
			JLabel aboutJLabel = new JLabel(new ImageIcon("image/about.png"));
			aboutJLabel.setBounds(0, 0, 258, 258);
			JDialog jDialog = new JDialog();
			jDialog.getContentPane().add(aboutJLabel);
			jDialog.setSize(344, 344);
			jDialog.setAlwaysOnTop(true);
			jDialog.setLocationRelativeTo(null);
			jDialog.setModal(true);
			jDialog.setVisible(true);
		} else if (obj==girls) {
			Random rd = new Random();
			int index=rd.nextInt(13)+1;
			path="image\\girl\\girl"+index+"\\";
			initData();
			initImage();
		}else if (obj==animal) {
			Random rd = new Random();
			int index=rd.nextInt(8)+1;
			path="image\\animal\\animal"+index+"\\";
			initData();
			initImage();
		}else if (obj==sports) {
			Random rd = new Random();
			int index=rd.nextInt(10)+1;
			path="image\\sport\\sport"+index+"\\";
			initData();
			initImage();
		}
	}
}
